package com.tiza.xiaoyoubao.network;

import com.tianze.library.utils.Base64;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.util.HashMap;

/**
 * Created by coderlyzz on 2017/9/11 0011.
 * 浏览任务接口
 */

public class BrowserTaskApi {


    //操作任务
    public static void optionBrowserTask(String cid,
                                         String orderid,
                                         String buyerid,
                                         File imageSearchResult,//搜索结果
                                         File imageGoodsHeader,//商品头部
                                         File imageGoodsFooter,//商品尾部
                                         File imageStoreGoods,//收藏目标商品
                                         File imageAddShoppingcart,//加入购物车
                                         File imageStoreShop,//收藏商品店铺
                                         String addtime,
                                         Callback callback) {//开始时间

        HashMap<String, String> params = new HashMap<>();
        params.put("cid", cid);
        params.put("orderid", orderid);
        params.put("buyerid", buyerid);
        params.put("addtime", addtime);
        PostFormBuilder pfb = OkHttpUtils.post();
        if (imageSearchResult != null) {
            pfb.addFile("imageSearchResult", "imageSearchResult.jpg", imageSearchResult);
        }
        if (imageGoodsHeader != null) {
            pfb.addFile("imageGoodsHeader", "imageGoodsHeader.jpg", imageGoodsHeader);//
        }
        if (imageGoodsFooter != null) {
            pfb.addFile("imageGoodsFooter", "imageGoodsFooter.jpg", imageGoodsFooter);//
        }
        if (imageStoreGoods != null) {
            pfb.addFile("imageStoreGoods", "imageStoreGoods.jpg", imageStoreGoods);//
        }
        if (imageAddShoppingcart != null) {
            pfb.addFile("imageAddShoppingcart", "imageAddShoppingcart.jpg", imageAddShoppingcart);//
        }
        if (imageStoreShop != null) {
            pfb.addFile("imageStoreShop", "imageStoreShop.jpg", imageStoreShop);//
        }

        pfb.url(Constants.OptionBrowserTaskUrl).params(Base64Encode(params)).build()//
                .execute(callback);
    }


    private static HashMap<String, String> Base64Encode(HashMap<String, String> params) {
        HashMap<String, String> base64Params = new HashMap();
        for (String key : params.keySet()) {
            Object object = params.get(key);
            base64Params.put(key, Base64.encodeBase64Url(String.valueOf(object)));
        }
        return base64Params;
    }
}
